package com.alks.function.service.serviceImpl.finance.salary;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.PcBonusEndMonth;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.finance.salary.PcBonusEndMonthRequest;
import com.alks.function.mapper.finance.salary.SealAccountMapper;
import com.alks.function.service.finance.salary.SealAccountService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class SealAccountServiceImpl implements SealAccountService {

    @Autowired
    private SealAccountMapper sealAccountMapper;

    /**
     * 查询员工产值核算封账月份明细
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getProductAccountDetails(PcBonusEndMonthRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<PcBonusEndMonth> detailsList = sealAccountMapper.getProductAccountDetails(companyId,request);

        PageInfo list = new PageInfo(detailsList);
        HashMap map = new HashMap<>();
        map.put("detailsList", detailsList);
        map.put("total", list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 编辑：员工产值核算封账月份明细
     * @return
     */
    @Override
    public ResponseInfo updateProductAccountDetails(PcBonusEndMonthRequest request) {
        if(request.getBonusYM() == null && request.getBonusType() == null) {
            return ResponseInfo.error("封账月份与类型不能为空！");
        }
        if(request.getBonusYM().length() > 6){
            return ResponseInfo.error("封账月份长度不能超过6位！");
        }
        String companyId = UserIdThread.get().getCompanyId();
        request.setCompanyId(companyId);
        String type = request.getBonusType();
        if (type.equals("针车")) {
            request.setBonusType("STITCH");
        }else if(type.equals("裁断")){
            request.setBonusType("CUT");
        }else{
            return ResponseInfo.error("类型错误！");
        }
        int count = sealAccountMapper.updateProductAccountDetails(request);
        if(count > 0){
            return ResponseInfo.ok("更改成功！");
        }else {
            return ResponseInfo.error("更改失败！");
        }
    }

    /**
     * 添加数据：员工产值核算封账月份明细
     * @param request
     * @return
     */
    @Override
    public ResponseInfo addProductAccountDetails(PcBonusEndMonthRequest request) {
        if(request.getBonusYM() == null && request.getBonusType() == null) {
            return ResponseInfo.error("封账月份与类型不能为空！");
        }
        if(request.getBonusYM().length() > 6){
            return ResponseInfo.error("封账月份长度不能超过6位！");
        }
        String companyId = UserIdThread.get().getCompanyId();
        String userName = UserIdThread.get().getUserName();
        String type = request.getBonusType();
        if (type.equals("针车")) {
            request.setBonusType("STITCH");
        }else if(type.equals("裁断")){
            request.setBonusType("CUT");
        }

        //判断有没有重复
        String key = companyId + request.getBonusYM() + request.getBonusType();
        List<PcBonusEndMonth> detailsList = sealAccountMapper.getProductAccountDetails(companyId,request);
        if(ObjectUtils.isNotEmpty(detailsList)){
            return ResponseInfo.error("该封账月份和类型已存在！");
        }

        PcBonusEndMonth pcBonusEndMonth = new PcBonusEndMonth();
        BeanUtils.copyProperties(request, pcBonusEndMonth);
        pcBonusEndMonth.setSysUser(userName);
        pcBonusEndMonth.setCompanyId(companyId);

        int count = sealAccountMapper.addProductAccountDetails(pcBonusEndMonth);
        if(count > 0){
            return ResponseInfo.ok("新增成功！");
        }else {
            return ResponseInfo.error("新增失败！");
        }
    }

    /**
     * 删除：员工产值核算封账月份明细
     * @param request
     * @return
     */
    @Override
    public ResponseInfo deleteProductAccountDetails(PcBonusEndMonthRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        String type = request.getBonusType();
        if (type.equals("针车")) {
            request.setBonusType("STITCH");
        }else if(type.equals("裁断")){
            request.setBonusType("CUT");
        }
        PcBonusEndMonth pcBonusEndMonth = new PcBonusEndMonth();
        BeanUtils.copyProperties(request, pcBonusEndMonth);
        pcBonusEndMonth.setCompanyId(companyId);
        int count = sealAccountMapper.deleteProductAccountDetails(pcBonusEndMonth);
        if(count > 0){
            return ResponseInfo.ok("删除成功！");
        }else {
            return ResponseInfo.error("删除失败！");
        }
    }
}